app.controller("userController",function ($scope,$controller,userService,uploadService) {

    $controller("baseController",{$scope:$scope});

    $scope.searchEntity = {'isSend':null};

    //获取签约用户信息
    $scope.findPage = function (pageNum, pageSize) {

        if (pageNum == null) pageNum = 1;

        if (pageSize == null) pageSize = 10;

        if (startTime != null) $scope.searchEntity.startTime = startTime;

        if (endTime != null) $scope.searchEntity.endTime = endTime;


        userService.findPage(pageNum,pageSize,$scope.searchEntity).success(function (response) {

            if (response.code == 200){

                $scope.userList = response.data;

                $scope.paginationConf.totalItems=response.total;
            } else {
                alert(response.msg);
            }
        })
    };

    //添加签约明细列表
    $scope.addSignedRecordList = function (signedRecordList) {
        console.log(JSON.stringify(signedRecordList));
        $scope.signedRecordList = signedRecordList;

    };

    //重置条件查询参数
    $scope.resetSearchEntity = function () {

        $scope.searchEntity = {};

        $scope.reloadList();

    };

    //监控认证状态
    $scope.$watch("searchEntity.authenticationStatus",function (newValue, oldValue) {

        if (newValue == null) return;

        $scope.reloadList();
    });

    //监控签约状态
    $scope.$watch("searchEntity.signedStatus",function (newValue, oldValue) {

        if (newValue == null)
            return;

        $scope.reloadList();

    });

    //监控短信状态
    $scope.$watch("searchEntity.isSend",function (newValue, oldValue) {

        console.log("短信状态变量值：" + newValue);

        if (newValue == null)
            return;

        $scope.reloadList();

        console.log("列表刷新");

    });
    //删除一个签约用户 - 真删除
    $scope.delete = function (index) {

        if (!confirm("是否确认删除？(若无信息错误，请不要自行删除已签约信息，否则后果自行承担)"))
            return;
        var user = $scope.userList[index];

        userService.delete(user).success(function (reponse) {

            if (reponse.code == 200){

                alert("删除成功");

                $scope.reloadList();

            } else {
                alert(reponse.msg);
            }
        })
    }

    $scope.uploadParam = {"frontIdCard": null,"backIdCard":null, "userId": null, "businessLicense": null,"companyId":null, "paperContractUrl": null, "signedRecordId": null};

    $scope.prompt = "请上传";

    //上传身份证
    $scope.uploadIdCard = function (type) {
        console.log("上传执行")
        $scope.prompt = "上传中";
        if (type == 1) {
            //正面
            uploadService.uploadFrontIdCard().success(function (response) {

                if (response.code == 200) {

                    $scope.uploadParam.frontIdCard = response.data;

                    $scope.prompt = response.msg;

                } else {
                    $scope.prompt = response.msg;
                    alert(response.msg);
                }

            });
        } else if (type == 2) {
            //反面
            uploadService.uploadBackIdCard().success(function (response) {

                if (response.code == 200) {

                    $scope.uploadParam.backIdCard = response.data;

                    $scope.prompt = response.msg;

                } else {
                    $scope.prompt = response.msg;
                    alert(response.msg);
                }

            });
        }

    };

    $scope.uploadUserBusinessLicenseFile = function() {
        uploadService.uploadUserBusinessLicense($scope.uploadParam).success(res => {
            if (res.code == 200) {
                $scope.uploadParam.businessLicense = res.data;
                $scope.prompt = res.msg
            } else {
             $scope.prompt = res.msg;
             alert(res.msg)
            }
        })
    }

    //上传身份证时添加用户id
    $scope.addUserId = function (userId,companyId) {
        console.log("添加参数执行")
        $scope.uploadParam.userId = userId;
        $scope.uploadParam.companyId = companyId;

    };
    $scope.addUserIdSignedRecordId = function (userId,signedRecordId) {
        $scope.uploadParam.userId = userId;
        $scope.uploadParam.signedRecordId = signedRecordId;

    };

    //添加身份证
    $scope.addIdCard = function () {

        if ($scope.isNull($scope.uploadParam.frontIdCard)) {
            alert("未上传身份证正面");
            return;
        }
        if ($scope.isNull($scope.uploadParam.backIdCard)) {
            alert("未上传身份证反面");
            return;
        }

        userService.addIdCard($scope.uploadParam).success(function (response) {

            if (response.code == 200) {

                $scope.reloadList();
                $scope.prompt = "请上传";
            } else {

                alert(response.msg);
            }
        });

    };

    //添加身份证
    $scope.addUserBusinessLicenseFile = function () {

        if ($scope.isNull($scope.uploadParam.businessLicense)) {
            alert("未上传营业执照");
            return;
        }

        userService.addBusinessLicense($scope.uploadParam).success(function (response) {

            if (response.code == 200) {

                $scope.reloadList();
                $scope.prompt = "请上传";
            } else {

                alert(response.msg);
            }
        });

    };

    /**
     * 签约记录批量导出
     */
    $scope.signExport = function () {

        if (!confirm("确定导出吗？")) {
            return;
        }

        userService.signExport($scope.searchEntity).success(function (res) {
            if (res.code == 200) {
                window.open(res.data);
            } else {
                alert(res.msg);
            }

        });
    }

    /**
     * 注销个体户
     * @param userId
     */
    $scope.cancellation = function (userId,selfEmployedStatus) {

        if (!confirm("确认注销此个体户吗？")){
            return
        }

        userService.cancellation(userId,selfEmployedStatus).success(function (res) {
            if (res.code == 200) {
                $scope.reloadList();
            }else {
                alert(res.msg)
            }
        })
    }

    $scope.uploadPaperContractFile = function() {
        uploadService.uploadPaperContract().success(res => {
            if (res.code == 200) {
                $scope.uploadParam.paperContractUrl = res.data;
                $scope.prompt = res.msg
            } else {
                $scope.prompt = res.msg;
                alert(res.msg)
            }
        })
    }

    $scope.addPaperContractUrl = function() {
        userService.addPaperContract($scope.uploadParam).success(res => {
            if (res.code == 200) {
                $scope.reloadList();
            } else {
                alert(res.msg);
            }
        })
    }

    $scope.downloadContractFile = function(flowId) {
        userService.downloadContract({flowId: flowId}).success(res => {
            if (res.code == 200) {
                window.open(res.data);
            } else {
                alert(res.msg);
            }
        })
    }

    $scope.downloadContractFiles = function() {
        let urls = []
        for (let i = 0; i < $scope.userList.length; i++) {
            urls.push($scope.userList[i].signedRecord.flowId)
        }
        userService.downloadContractUrls(urls).success(res => {
            if (res.code == 200 && res.data) {
                window.open(res.data);
            } else {
                alert("暂无可用下载");
            }
        })
    }

    $scope.downloadContractSignUrl = function(url) {
        window.open(url);
    }
});